﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PuzzleRoomManager : MonoBehaviour {

	public PuzzleRoomData puzzleRoomData;
	public SampleInventoryHandler sampleInventoryHandler;
	public List<GameObject> items;
	public List<GameObject> activeBGs;
	public CameraManager cameraManager;
	public MusicManager musicManager;

	[HideInInspector]
	public bool mouseActive;

	// Use this for initialization
	void Awake(){
		puzzleRoomData = GameObject.Find("PuzzleRoomManager").GetComponent<PuzzleRoomData>();
		puzzleRoomData.Mouse = GameObject.Find("Mouse");
		sampleInventoryHandler = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>();
		cameraManager = GameObject.Find(this.name).GetComponent<CameraManager>();
		musicManager = GameObject.Find("MenuManager").GetComponent<MusicManager>();
	}

	void Start () {
		musicManager.PlayBGM(Resources.Load("BGM/puzz1", typeof(AudioClip)) as AudioClip);
		Dialoguer.events.onMessageEvent += OnMessageEvent;
		HideItems();
		mouseActive = false;
		for(int i = 0; i < activeBGs.Count; i++)
			activeBGs[i].SetActive(false);
	}

	void OnMessageEvent (string message, string metadata)
	{
		switch(message)
		{
			case "AddInventory":
			AddInventory(int.Parse(metadata));
			break;
			case "UpdateProfile":
			UpdateProfile(metadata);
			break;
			case "RemoveObjectFromScene":
			RemoveObjectFromScene(metadata);
			break;
			case "ReplaceObj1toObj2":
			ReplaceObjects(metadata);
			break;
			case "ShowItem":
			ShowItem(metadata);
			break;
			case "HideItems":
			HideItems();
			break;
			case "ChangeCamera":
			ChangeCamera(int.Parse(metadata));
			break;
			case "SetActiveBG":
			SetActiveBG(metadata);
			break;
			default:
			return;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(mouseActive)
		{
			Camera cam = Camera.main;
			Vector3 v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 9);
			v3 = cam.ScreenToWorldPoint(v3);
			puzzleRoomData.Mouse.transform.position = v3;
		}
	}

	void AddInventory(int itemID)
	{
		sampleInventoryHandler.playerInventory.Add(ItemHelper.GetItemByID(itemID));
	}

	void UpdateProfile(string text)
	{
		string[] strings = text.Split(new char[]{','});
		for(int i = 0; i < sampleInventoryHandler.profiles.Count; i++)
			if(sampleInventoryHandler.profiles[i].name == strings[0])
			{
				sampleInventoryHandler.profiles[i].description = Profiles.UpdateDescription(sampleInventoryHandler.profiles, strings[0], strings[1]);
				return;
			}

		sampleInventoryHandler.profiles.Add(Profiles.AddProfile(strings[0]));
		sampleInventoryHandler.profiles.Find(x => x.name == strings[0]).description = Profiles.UpdateDescription(sampleInventoryHandler.profiles, strings[0], strings[1]);
	}

	void RemoveObjectFromScene(string objectName)
	{
		GameObject.Find (objectName).renderer.enabled = false;
		GameObject.Find (objectName).collider2D.enabled = false;
	}

	void ReplaceObjects(string text)
	{
		string[] strings = text.Split(new char[]{','});
		Destroy(GameObject.Find(strings[0]));
		if(GameObject.Find(strings[1]).activeSelf == false)
			GameObject.Find(strings[1]).SetActive(true);
	}

	void ShowItem(string meta)
	{
		items.Find(x => x.name == meta).SetActive(true);
	}

	void HideItems()
	{
		for(int i = 0; i < items.Count; i++)
			items[i].SetActive(false);
	}

	void ChangeCamera(int cameraIndex)
	{
		if(cameraIndex == 1 || cameraIndex == 4 || cameraIndex == 5)
			mouseActive = true;
		else
			mouseActive = false;
		cameraManager.changeCamera(cameraIndex);
	}

	void SetActiveBG(string bgName)
	{
		string[] strings = bgName.Split (new char[]{','});
		if(strings.Length > 1)
		{
			activeBGs.Find(x => x.name == strings[0]).SetActive(true);
			for(int i = 0; i < activeBGs.Count; i++)
				if(activeBGs[i].name == strings[1])
				{
					activeBGs.Find(x => x.name == strings[1]).SetActive(false);
					return;
				}
			GameObject.Find(strings[1]).SetActive (false);
		}
		else
		{
			activeBGs.Find(x => x.name == strings[0]).SetActive(true);
		}
	}
}